package com.threeti.mecool.core.domain.model.plan.pojo;

import com.threeti.mecool.core.infrastructure.persistence.plan.StepRepository;
import org.codehaus.jackson.annotate.JsonBackReference;
import org.codehaus.jackson.annotate.JsonManagedReference;
import org.springframework.beans.factory.annotation.Configurable;

import javax.annotation.Resource;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Configurable
@Entity
@Table(name = "DM_WF_STEP")
public class Step {
    private Long id;
    private Integer version;
    private Task task;
    private String name;
    private int fixed;
    private int optional;
    private Job job;
    private int stepsOrder;
    private List<StepItem> stepItems = new ArrayList<StepItem>();
    private StepItem stepItem;
    private StepRepository repository;

    public static Step findById(Long stepId) {
        return repository().findOne(stepId);
    }

    public void save() {
        repository.save(this);
    }

    public void createStepItems() {
        if (task == null) {
            throw new RuntimeException("task has not been assigned");
        }
        stepItems.clear();
        for (TaskItem taskItem : task.getTaskItems()) {
            StepItem stepItem = new StepItem();
            stepItem.setTaskItem(taskItem);
            stepItem.setStep(this);
            stepItems.add(stepItem);
        }
    }

    //<editor-fold desc="get/set">
    private static StepRepository repository() {
        return new Step().repository;
    }

    @Resource
    public void setRepository(StepRepository stepRepository) {
        this.repository = stepRepository;
    }

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    @Version
    public Integer getVersion() {
        return version;
    }

    @ManyToOne
    public Task getTask() {
        return task;
    }

    public String getName() {
        return name;
    }

    public int getFixed() {
        return fixed;
    }

    public int getStepsOrder() {
        return stepsOrder;
    }

    @JsonBackReference("job")
    @ManyToOne
    public Job getJob() {
        return job;
    }

    public int getOptional() {
        return optional;
    }

    @JsonBackReference("stepItem")
    @ManyToOne
    public StepItem getStepItem() {
        return stepItem;
    }

    @JsonManagedReference("step")
    @OneToMany(mappedBy = "step", cascade = CascadeType.ALL, orphanRemoval = true)
    public List<StepItem> getStepItems() {
        return stepItems;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public void setTask(Task task) {
        this.task = task;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setFixed(int fixed) {
        this.fixed = fixed;
    }

    public void setStepsOrder(int stepsOrder) {
        this.stepsOrder = stepsOrder;
    }

    public void setJob(Job job) {
        this.job = job;
    }

    public void setOptional(int optional) {
        this.optional = optional;
    }

    public void setStepItem(StepItem stepItem) {
        this.stepItem = stepItem;
    }

    public void setStepItems(List<StepItem> stepItems) {
        this.stepItems = stepItems;
    }
    //</editor-fold>
}
